﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using System.Web.Http;
using Jw.Models;
using Jw.Web.Areas.Dev.Common;
using Jw.Core;
using Newtonsoft.Json.Linq;
using Jw.Web.App_Start;

namespace Jw.Web.Areas.Dev.Controllers
{
    public class BdjlController : BaseController
    {
        //
        // GET: /Dev/Bdjl/

        public ActionResult Index()
        {
            var codeService = new sys_codeService();
            var model = new
            {
                idField = "Bdjl_BillNo", //主键
                urls = DevHelper.GetIndexUrls("Bdjl"),
                resx = DevHelper.GetIndexResx("设备变动记录"),
                dataSource = new
                {
                    yearItems = codeService.GetYearItems(),
                    monthItems = codeService.GetMonthItems()
                },
                form = new
                {
                    Bdjl_BillNo = "",
                    Bdjl_Sbbh = ""
                }

            };
            return View(model);
        }
         [MvcMenuFilter(false)]
        public ActionResult Edit(string id = "")
        {
            var UserName = FormsAuth.GetUserData().UserName;
            var data = new BdjlApiController().GetEditMaster(id);
            var codeService = new sys_codeService();

            var model = new
            {
                form = data.form,
                scrollKeys = data.scrollKeys,
                urls = DevHelper.GetEditUrls("Bdjl"),
                resx = DevHelper.GetEditResx("设备变动记录"),
                dataSource = new
                {
                },
                defaultForm = new b_Bdjl().Extend(new
                {
                    Bdjl_BillNo = id,
                    CreatePerson = UserName,
                    UpdatePerson = UserName,
                    Bdjl_Ywrq = DateTime.Now,
                    CreateDate = DateTime.Now.ToShortDateString(),
                    UpdateDate = DateTime.Now.ToShortDateString(),
                    Bdjl_Ybmbh = "",
                    Bdjl_Bmbh = ""
                }),
                defaultRow = new
                {
                },
                setting = new
                {
                    postFormKeys = new string[] { "Bdjl_BillNo" },
                    postListFields = new string[] { }
                }
            };
            return View(model);
        }

    }


    public class BdjlApiController : ApiController
    {
        public dynamic Get(RequestWrapper query)
        {
            query.LoadSettingXmlString(@"
            <settings defaultOrderBy='Bdjl_BillNo'>
                <select>
                   a.*,b.Sbda_Sbmc
                </select>
                <from>
                   b_Bdjl a left join b_Sbda b on a.Bdjl_Sbbh=b.Sbda_Sbbm
                </from>
                <where defaultForAll='true' defaultCp='equal' defaultIgnoreEmpty='true' >
                    <field name='Bdjl_BillNo' cp='like' ></field>
                    <field name='Bdjl_Jbry' cp='like' ></field>
                    <field name='A.ApprovePerson ' cp='daterange'></field>
                    <field name='Bdjl_Ywrq' cp='daterange'></field>
                </where>
            </settings>
            ");
            var pQuery = query.ToParamQuery();
            var list = new b_BdjlService().GetDynamicListWithPaging(pQuery);
            return list;

        }

        public List<dynamic> GetBillNo(string q)
        {
            var pQuery = ParamQuery.Instance().Select("distinct top 10 Bdjl_BillNo").AndWhere("Bdjl_BillNo", q, Cp.StartWith);
            return new b_BdjlService().GetDynamicList(pQuery);
        }

        public List<dynamic> GetDoNames(string q)
        {
            var pQuery = ParamQuery.Instance().Select("distinct top 10   Bdjl_Jbry").AndWhere("Bdjl_Jbry", q, Cp.StartWithPY);
            return new b_BdjlService().GetDynamicList(pQuery);
        }

        public List<dynamic> GetApNames(string q)
        {
            var pQuery = ParamQuery.Instance().Select("distinct top 10 ApprovePerson").AndWhere("ApprovePerson", q, Cp.StartWithPY);
            return new b_BdjlService().GetDynamicList(pQuery);
        }

        public string GetNewBillNo()
        {
            return new b_BdjlService().GetNewKey("Bdjl_BillNo", "dateplus");
        }
        public dynamic GetEditMaster(string id)
        {
            b_Bdjl obj = new b_BdjlService().GetModel(ParamQuery.Instance().AndWhere("Bdjl_BillNo", id));
            return new
            {
                form = obj,
                scrollKeys = new b_BdjlService().ScrollKeys("Bdjl_BillNo", id.ToString())
            };
        }
        public void Edit(dynamic data)
        {
            //   <columns ignore = 'Xhjh_BillNo'></columns>
            var formWrapper = RequestWrapper.Instance().LoadSettingXmlString(@"
            <settings>
                <table>{0}</table>
                <where><field name='Bdjl_BillNo' cp='equal'></field></where>
            </settings>", typeof(b_Bdjl).Name);

            var result = new b_BdjlService().Edit(formWrapper, null, data);
        }

        public void Delete(string id)
        {
            var result = new b_BdjlService().Delete(ParamDelete.Instance().AndWhere("Bdjl_BillNo", id));
            DevHelper.ThrowHttpExceptionWhen(result <= 0, "单据删除失败[Bdjl_BillNo={0}]，请重试或联系管理员！", id);
        }

        //审核
        [System.Web.Http.HttpPost]
        public void Audit(string id, JObject data)
        {
            var status = data["status"].ToString();
            var comment = data["comment"].ToString();
            var result = new DevService().Audit(typeof(b_Bdjl).Name, "Bdjl_BillNo", id, status, comment);
            DevHelper.ThrowHttpExceptionWhen(result < 0, "单据审核失败[Bdjl_BillNo={0}]，请重试或联系管理员！", id);
        }
    }
}

